#!/bin/sh

source /env.sh

echo -e "${BLUE}Executing /init script${NC}"
mount -t devtmpfs dev /dev	# devtmpfs is NOT auto-mounted by kernel in initramfs
mount -t proc proc /proc	# Must mount /proc to mount real root later

echo -e "${RED}Press ENTER to switch to real root${NC}"
read -t 1 ans	# 1 seconds delay to wait for input

if [ $? != 0 ]; then
	echo -e "${BLUE}Entering initramfs.${NC}"

	mkdir -p /dev/pts
	mount -t tmpfs /dev/pts

	ifconfig eth0 hw ether 12:34:56:78:90:FF
	udhcpc &

	dropbear -r /etc/dropbear_ed25519_host_key -RBE

	while [ true ]; do

		# Spawn shell with job control on ttyS0
		# For more info, see busybox cttyhack
		setsid sh -c 'exec sh </dev/ttyS0 >/dev/ttyS0 2>&1'
		echo -e "${BLUE}Shell respawned. Use 'reboot -f' or 'halt -f' to restart/shutdown the system.${NC}"

	done
else
	echo -e "${GREEN}Switching from initramfs to real root...${NC}"
	mount /dev/mmcblk0p2 /mnt
	mount --move /dev /mnt/dev
	mount --move /proc /mnt/proc
	exec switch_root -c /dev/ttyS0 /mnt /sbin/init
fi

